<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container  :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="教职工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-dict-select-tag type="list" :disabled="true" v-model="model.userId" dictCode="sys_user,realname,id" placeholder="请选择教工" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveType">
              <j-dict-select-tag type="list"  v-model="model.leaveType" dictCode="leave_type" placeholder="请选择请假类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date :showTime="true"  date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择请假开始时间" v-model="model.startTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date :showTime="true"  date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择请假结束时间" v-model="model.endTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="请假时长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="length">
              <a-input-number v-model="model.length" :min="0" suffix="天" placeholder="请输入请假时长" style="width: 100%;" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="请假事由" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveExplain">
              <a-textarea v-model="model.leaveExplain"  rows="4" placeholder="请输入请假事由" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvar">
              <!-- <j-dict-select-tag type="list"  v-model="model.approvar" dictCode="sys_user,realname,id" placeholder="请选择审批人" /> -->
              <a-select  v-model="model.approvar" placeholder="请选择审批人">
                <a-select-option v-for="item in approvalList" :key="item.id" :value="item.id">{{ item.realname }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.id && model.approvalState != 2">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalState">
              <a-input-number v-model="filterDictTextByCacheText" readonly placeholder="请输入审批状态" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <div v-if="disabled && model.approvalState != 1">
            <a-col :span="24" >
              <a-form-model-item label="审批时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalTime">
                <a-input v-model="model.approvalTime" ></a-input>
              </a-form-model-item>
            </a-col>
             <a-col :span="24">
              <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
                <a-input v-model="transformApprovalResult" placeholder="请输入审批结果" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
                <a-textarea v-model="model.approvalInfo" rows="4" placeholder="请输入审批信息" />
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-form-model :model="model"  v-if="approvlStatus && !disabled ">
      <a-row>
        <a-col :span="24">
          <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
            <j-dict-select-tag type="list" v-model="model.approvalResult" dictCode="approval_result"  placeholder="请选择审批结果" />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
            <a-textarea v-model="model.approvalInfo"  rows="4" placeholder="审批信息" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import Vue from 'vue'
  import { USER_INFO } from "@/store/mutation-types"
  import {filterDictTextByCache} from '@/components/dict/JDictSelectUtil'
  export default {
    name: 'SkStaffLeaveForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      status:Number
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          leaveType:[
            { required: true, message: '请选择请假类型!'},
          ],
          startTime:[
            { required: true, message: '请选择请假开始时间!'},
          ],
          endTime:[
            { required: true, message: '请选择请假结束时间!'},
          ],
          approvar:[
            { required: true, message: '请选择审批人!'},
          ],
        },
        url: {
          add: "/management/skStaffLeave/add",
          edit: "/management/skStaffLeave/edit",
          queryById: "/management/skStaffLeave/queryById"
        },
        approvlStatus:null,
        approvalList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.approvlStatus
      },
      filterDictTextByCacheText(){
        return filterDictTextByCache("approval_state",this.model.approvalState)
      },
      transformApprovalResult(){
        return filterDictTextByCache("approval_result",this.model.approvalResult)
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getApproval()
    },
    methods: {
      // 计算请假时长
      // onChangeDate(){
      //   let start = this.model.startTime
      //   let end = this.model.endTime
      //   if (!start || !end) return;
      //   let leaveDay = this.$skLeaveDay.getTotal(start,end)
      //   this.model.length = leaveDay.days + "." +  Math.round(leaveDay.hours)
      // },
      // 获取审批人
      getApproval(){
        // const url = "/management/teacher/list"
        const url = "/task/getApprovarList"
        getAction(url).then((res)=>{
          if(res.success){
            this.approvalList = res.result;
          } else {
            this.$message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record,approvlStatus) {
        console.log(record);
        // 获取当前登陆者id
        let user = Vue.ls.get(USER_INFO);
        if (record.id ) {
          if (record.userId == user.id) {
            this.model = {
              id:record.id,
              userId:record.userId,
              leaveType:record.leaveType,
              startTime:record.startTime,
              endTime:record.endTime,
              length:record.length,
              leaveExplain:record.leaveExplain,
              approvar:record.approvar,
              approvalState:record.approvalState,
              approvalInfo:record.approvalInfo,
              approvalTime:record.approvalTime,
              approvalResult:record.approvalResult,
            }
          }else{
            this.model = Object.assign({}, record);
          }
          
        }
        // 审核状态
        this.approvlStatus = approvlStatus;
        if (this.disabled) {
          this.approvlStatus = true;
        }
        
        if (!this.model.id) {
          this.model.userId = user.id;
        }
        
        console.log(this.model);
        this.visible = true;
      },
      approval (record,approvlStatus) {
        this.model = Object.assign({}, record);
        // 审核状态
        this.approvlStatus = approvlStatus;
        if (this.disabled) {
          this.approvlStatus = true;
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>
<style lang="less" scoped>

.ant-select-disabled{
  color:rgba(0, 0, 0, 0.65);
  /deep/ .ant-select-selection{
    background: #fff ;
  }
}
// /deep/ .ant-input-number{
//   position: relative;
// }
// /deep/ .ant-input-number::before{
//   content: "天";
//   position: absolute;
//   top: 0;
//   right: 32px;
//   height: 32px;
//   line-height: 32px;
//   color: #999;
//   font-size: 14px;
// }
</style>